package com.jinbei.restController;

import com.jinbei.common.annotation.AuthorAnnotation;
import com.jinbei.common.base.ResponseBase;
import com.jinbei.common.base.ResponseBaseEntity;
import com.jinbei.common.contants.Contants;
import com.jinbei.domain.Course;
import com.jinbei.service.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author zflu
 * 课程 restful 接口
 * Created by lucky on 2018/8/7.
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    private final Logger logger = LoggerFactory.getLogger(CourseController.class);

    @Autowired
    private CourseService courseService;

    /**
     * @author zflu
     * @Description   活动课程新增
     * @date 2018/8/7 17:09
     * @param course
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name="/course/insertCourse" ,logs = "活动课程新增")
    @PostMapping("/insertCourse")
    public ResponseBase insertCourse(@RequestBody Course course){
        try {
            int result = courseService.insertCourse(course);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("活动课程新增失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   活动课程修改
     * @date 2018/8/9 9:29
     * @param course
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name="/course/updateCourse" ,logs = "活动课程修改")
    @PutMapping("/updateCourse")
    public ResponseBase updateCourse(@RequestBody Course course){
        try {
            int result = courseService.updateCourse(course);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("活动课程修改失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   活动课程删除
     * @date 2018/8/9 9:29
     * @param type
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name="/course/deleteCourse" ,logs = "活动课程删除")
    @DeleteMapping("/deleteCourse")
    public ResponseBase deleteCourseByType(@RequestParam("type") String type){
        try {
            int result = courseService.deleteCourseByType(type);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("活动课程删除失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   活动课程查询
     * @date 2018/8/9 10:11
     * @param type
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name="/course/queryCourse" ,logs = "活动课程查询")
    @GetMapping("/queryCourse")
    public ResponseBase queryCourseByType(@RequestParam("type") String type){
        try {
            Course course = courseService.queryCourseByType(type);
            return ResponseBaseEntity.setResultSuccess(course);
        }catch (Exception e){
            logger.error("活动课程删除失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }
}
